﻿using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using WebSocket.Common;

namespace WebSocket.Server
{
    class Server
    {
        static void Main(string[] args)
        {
            ListenIPAddress lia = new ListenIPAddress();

            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Bind(lia.GetIPEndPoint());

            socket.Listen(0);
            Console.WriteLine("监听开始");

            Socket NewSocket = socket.Accept();
            Console.WriteLine("新建连接");

            string ClientMsg = string.Empty;
            byte[] msgByte = new byte[1024];
            int bytes;
            bytes = NewSocket.Receive(msgByte, msgByte.Length, 0);
            ClientMsg += Encoding.ASCII.GetString(msgByte, 0, bytes);

            Console.WriteLine("Client Message:" + ClientMsg);

            string ServerMsg = "this massage(" + ClientMsg + ") is successful";
            byte[] ServerByte = Encoding.ASCII.GetBytes(ServerMsg);

            NewSocket.Send(ServerByte);
            NewSocket.Close();
            socket.Close();
            Console.ReadLine();
        }
    }
}
